#include <stdio.h>
#include <map>
#include <string>
#include <string.h>
#include <stdlib.h>

#include "mzroute.h"
#include "mzhttp.h"


// the route_table
// {url => function}
// long need to convert before use
// function needs to return a char[] for c to use
std::map<std::string, long> route_table;


// add a route to route_table
void add_route(char* path, long func){
	printf("add route for %s, func at %ld \n", path, func);
	route_table.emplace(path, func);
}

long serve(char* path){
	printf("serve %s \n", path);
	// find route
	std::map<std::string, long>::iterator it = route_table.find(path);
	long func = 0;
	if(it != route_table.end()){
        func = it->second;
		printf("find func: %ld \n", func);
		return func;
	}else{
		printf("not found \n");	
		return 0;
	}

}

char* _index(mz_http_headers* headers){
	char* ret = NULL;
	char* name = NULL;
	char default_name[] = "nobody";
	ret = (char*)malloc(255);             // need to free
	name = get_args_value(&headers->args, "name");
	if (name == NULL){
		name = default_name;
	}
	sprintf(ret, "hello %s, here is _index!\n<br>@Mz1\n", name);
	return ret;    // need to free outside
}

